<?php

namespace app\admin\controller;

use app\admin\traits\Result;
use think\Controller;
use think\Request;
use app\admin\model\Article as A;

class Article extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function articleList()
    {
        //
        if ($this->request->isAjax()){
            $data = [
                'key'=>$this->request->get('key','','trim'),
                'limit'=>$this->request->get('limit','','intval')
            ];
            //搜索器

            $list = A::withSearch(['name'],['name'=>$data['key']])->where('status','neq',-1)->order('id','desc')->paginate($data['limit'],false,['query'=>$data]);
            $user_date = [];
            foreach ($list as $key => &$val) {
                $val['category_name'] = getCategory($val['category_id']);
                $user_date[$key] = $val;

            }
            return show($user_date,0,'',['count'=>$list->total()]);

        }
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        //
        if ($this->request->isPost()){
            $data = $this->request->post();

            if (isset($data['id'])){
                //编辑

                $msg  = \app\facade\Article::edit($data);

            }else{
                //添加
                $msg = \app\facade\Article::add($data);
            }
            return $msg;
        }else{
            //如果是编辑，获得id
            $id = $this->request->get('id','','intval');
            if (!empty($id)){
               $list = A::where(['id'=>$id])->find();
               $this->assign('list',$list);
            }
            //获得所有栏目
            $article_category = app('articleCategory');
            $categoryList = $article_category->getAllCategory();
            $this->assign('category_list',$categoryList);
            return $this->fetch();
        }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function setStatus($id,$status,$object = 'app\admin\model\Article'){
        $change_status = $status == 1 ? 0:1;
       $article = $object::where(['id'=>$id])->update(['status'=>$change_status]);
       if ($article !== false){
           return Result::success('操作成功');
       }else{
           return Result::error('操作失败');
       }
    }


}
